package com.jckl.foodathon.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.jckl.foodathon.R;
import com.jckl.foodathon.activities.HomeActivity;
import com.jckl.foodathon.model.Coordinate;
import com.jckl.foodathon.model.StoresListWidget;
import com.jckl.foodathon.services.location.NearbyStoreQuery;
import com.jckl.foodathon.widget.StoresListAdapter;

public class NearbyStoreFragment extends Fragment implements OnItemClickListener{
	
	ListView store_result_list;
	Coordinate coordinate;
	List<StoresListWidget> displayStores;

	public NearbyStoreFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_nearby_store, container, false);
		
		store_result_list	= (ListView) rootView.findViewById(R.id.store_result_list);
		coordinate 			= ((HomeActivity)getActivity()).getCoordinate();
		
		store_result_list.setOnItemClickListener(this);
		
		List<StoresListWidget> storeResultWidget = new ArrayList<StoresListWidget>();
		StoresListAdapter adapter = new StoresListAdapter(getActivity().getApplicationContext(), storeResultWidget);

		NearbyStoreQuery storeQuery = new NearbyStoreQuery();
		storeQuery.getNearbyStores(getActivity(), rootView, coordinate, storeResultWidget, adapter);
		
		displayStores = storeResultWidget;
		
		
		return rootView;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		StoresListWidget store = displayStores.get(arg2);
		((HomeActivity)getActivity()).setStoreName(store.getStoreName());
		
    	FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    	ft.replace(R.id.home_placeholder, new BlogListFragment());
    	ft.addToBackStack(null);
		ft.commit();
		
	}

}